<?php
/**
 * CreateTime: 2022/5/17 16:15
 */

namespace App\ServiceChecker\newContract;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Models\Contract\ContractTypeModel;
use App\Models\NewContract\ContractObjectModel;
use App\ServiceChecker\BasicServiceChecker;
use Illuminate\Http\Exceptions\HttpResponseException;

class ContractObjectLegitimacy extends BasicServiceChecker
{
    private ContractObjectModel $contractObjectModel;
    private ContractTypeModel $contractTypeModel;

    public function __construct()
    {
        parent::__construct();
        $this->contractObjectModel = new ContractObjectModel();
        $this->contractTypeModel = new ContractTypeModel();
    }

    public function verifyAddContractObject($contractObject)
    {
        $isExist = $this->contractObjectModel->isExistByCondition([
            'contract_type_id' => $contractObject['contract_type_id'],
            'status' => BaseConsts::STATUS_NORMAL,
            'company_id' => $contractObject['company_id'],
        ]);
        if($isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                '当前合同标的模板类型已存在',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
        $isExist = $this->contractObjectModel->isExistByCondition([
            'contract_type_id' => $contractObject['contract_type_id'],
            'contract_object_name' => $contractObject['contract_object_name'],
            'company_id' => $contractObject['company_id'],
            'status' => BaseConsts::STATUS_NORMAL,
        ]);
        if($isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                '当前合同标的模板已存在',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
        $this->verifyCompanyIsExists($contractObject['company_id']);
        $this->verifyContractTypeIsExists($contractObject['contract_type_id']);
    }

    public function verifyContractTypeIsExists($contractTypeId){
        $isExist = $this->contractTypeModel->isExistByCondition([
            'contract_type_id' => $contractTypeId,
            'status' => BaseConsts::STATUS_NORMAL,
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                '当前合同类型不存在或已删除',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    public function verifyContractObjectEdit($name,$id){
        $setNames = $this->contractObjectModel->setGetEditCheckerNames('contract_object_name',$name);
        $setIds = $this->contractObjectModel->setGetEditCheckerIds('contract_object_id',$id);
        // 获取当前信息
        $contractObjectInfo = $this->contractObjectModel->getContractObjectInfoByContractObjectId($id);
        $isExist = $this->contractObjectModel->editNameChecker(
            $setNames,
            $setIds,
            [
               $this->contractObjectModel->setGetEditCheckerCondition('company_id',$contractObjectInfo['company_id']),
               $this->contractObjectModel->setGetEditCheckerCondition('contract_type_id',$contractObjectInfo['contract_type_id'])
            ]
        );
        if($isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                '当前合同标的模板已存在',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }
}